package com.zbkj.service.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.dal.dao.OrderRentOverduePaymentRecordMapper;
import com.zbkj.dal.entity.order.OrderRentPaymentRecord;
import com.zbkj.service.model.dto.OrderRentOverduePaymentRecordCreateDTO;
import com.zbkj.service.service.OrderRentPaymentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderRentPaymentServiceImpl extends ServiceImpl<OrderRentOverduePaymentRecordMapper, OrderRentPaymentRecord> implements OrderRentPaymentService {

    @Override
    public String create(OrderRentOverduePaymentRecordCreateDTO createDTO) {
        OrderRentPaymentRecord orderRentPaymentRecord = new OrderRentPaymentRecord();
        orderRentPaymentRecord.setUid(createDTO.getUid());
        orderRentPaymentRecord.setPayOrderNo(IdUtil.getSnowflakeNextIdStr());
        orderRentPaymentRecord.setOrderNo(createDTO.getOrderNo());
        orderRentPaymentRecord.setRentRecordCode(createDTO.getRentRecordCode());
        orderRentPaymentRecord.setPayTradeType(createDTO.getPayTradeType());
        orderRentPaymentRecord.setTotalAmount(createDTO.getTotalAmount());
        orderRentPaymentRecord.setIntegralAmount(createDTO.getIntegralAmount());
        orderRentPaymentRecord.setOverdueAmount(createDTO.getOverdueAmount());
        orderRentPaymentRecord.setPayAmount(createDTO.getPayAmount());
        orderRentPaymentRecord.setPayTime(null);
        orderRentPaymentRecord.setPayType(createDTO.getPayType());
        orderRentPaymentRecord.setPayMethod(createDTO.getPayMethod());
        orderRentPaymentRecord.setPayChannel(createDTO.getPayChannel());
        orderRentPaymentRecord.setPaidStatus(createDTO.getPaidStatus());
        orderRentPaymentRecord.setRefundStatus(createDTO.getRefundStatus());
        orderRentPaymentRecord.setOutTradeNo(null);
        orderRentPaymentRecord.setRedirect(null);
        orderRentPaymentRecord.setIsDel(0);
        orderRentPaymentRecord.setCreateTime(LocalDateTime.now());
        orderRentPaymentRecord.setUpdateTime(LocalDateTime.now());
        if (this.save(orderRentPaymentRecord)) {
            return orderRentPaymentRecord.getPayOrderNo();
        }
        return StringUtils.EMPTY;
    }

    @Override
    public List<OrderRentPaymentRecord> getByOrderNo(String orderNo, OrderRentPaymentRecord.PayTradeType payTradeType, OrderRentPaymentRecord.PayType payType) {
        LambdaQueryWrapper<OrderRentPaymentRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(OrderRentPaymentRecord::getPayOrderNo, orderNo);
        lambdaQueryWrapper.eq(OrderRentPaymentRecord::getPayTradeType, payTradeType.name());
        lambdaQueryWrapper.eq(OrderRentPaymentRecord::getPayType, payType.name());
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public List<OrderRentPaymentRecord> getByOrderNo(String orderNo) {
        LambdaQueryWrapper<OrderRentPaymentRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(OrderRentPaymentRecord::getOrderNo, orderNo);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public OrderRentPaymentRecord getByPayOrderNo(String payOrderNo) {
        LambdaQueryWrapper<OrderRentPaymentRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(OrderRentPaymentRecord::getPayOrderNo, payOrderNo);
        return getOne(lambdaQueryWrapper);
    }

    @Override
    public OrderRentPaymentRecord getByOutTradeNo(String outTradeNo) {
        LambdaQueryWrapper<OrderRentPaymentRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(OrderRentPaymentRecord::getOutTradeNo, outTradeNo);
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public boolean updateById(OrderRentPaymentRecord entity) {
        return super.updateById(entity);
    }
}
